package com.czp.ssm.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.czp.ssm.bean.Msg;
import com.czp.ssm.bean.Plane;
import com.czp.ssm.bean.PlaneOrder;
import com.czp.ssm.bean.Train;
import com.czp.ssm.bean.TrainOrder;
import com.czp.ssm.service.TrainService;

@Controller
@RequestMapping("/trainController")
public class TrainController {
	
	@Autowired
	private TrainService trainService;
	
	/**
	 * 转向火车票页面
	 * @return   
	 */
	@RequestMapping("/toTrainIndex")
	public String toTrainIndex() {
		return "/foreground/train/train_index";
	}
	
	/**
	 * 列车信息
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/toTrainList" , method=RequestMethod.GET)
	public ModelAndView toTrainList(Train train) {
		ModelAndView modelAndView = new ModelAndView();
		List<Train> trainList = trainService.getTrainListByCondition(train);
		modelAndView.setViewName("/foreground/train/train_List");
		modelAndView.addObject(train);
		modelAndView.addObject(trainList);
		return modelAndView;
	}
	
	/**
	 * 根据条件查询对应的班次信息
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/findTrainList" , method=RequestMethod.POST)
	@ResponseBody
	public Msg findPlaneList(Train train) {
		List<Train> trainList = trainService.getTrainListByCondition(train);
		return Msg.success().add("trainList", trainList);
	}
	
	/**
	 * 根据条件查询对应的航班信息
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/getTrainOrderEdit" , method=RequestMethod.GET)
	public ModelAndView getTrainOrderEdit(@RequestParam("trainId")String trainId,@RequestParam("positionId")String positionId) {
		System.out.println("trainID------------"+trainId);
		System.out.println("positionId------------"+positionId);
		ModelAndView modelAndView = new ModelAndView();
		Train train = trainService.getTrainByCondition(trainId, positionId);
		modelAndView.addObject("train", train);
		modelAndView.setViewName("/foreground/train/train_book");
		return modelAndView;
	}
	
	/**
	 * 提交订单
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/submitTrainOrder" , method=RequestMethod.POST)
	@ResponseBody
	public Msg submitTrainOrder(TrainOrder trainOrder) {
		int flag = trainService.saveTrainOrder(trainOrder);
		if (flag<0) {
			return Msg.success();
		}
		return Msg.fail();
	}
	
	/**
	 * 火车票订单列表
	 * @param user
	 * @return
	 */
	@RequestMapping("/toTrainOrderList")
	public ModelAndView toTrainOrderList() {
		List<TrainOrder> trainOrderList = trainService.findAllPlaneOrders();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("trainOrderList",trainOrderList);
		modelAndView.setViewName("/foreground/train/train_orderList");
		return modelAndView;
	}
	
	
}
